package com.ftg.learn.rbac.controller;

import com.ftg.learn.rbac.service.IPermissionService;
import com.ftg.learn.rbac.service.IUserService;
import com.ftg.learn.rbac.vo.PermissionVo;
import com.ftg.learn.util.ResEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;

/**
 * @author Guo
 * @date 2022/2/9
 */
@Api(tags = "登录功能模拟")
@RestController
@RequestMapping("/login")
public class LoginController {

    @Resource
    IUserService iUserService;

    @Autowired
    IPermissionService iPermissionService;

    /**
     * 登入
     * @return
     */
    @ApiOperation(value = "登录功能")
    @PostMapping("/in")
    public ResEntity logIn(@RequestParam String username, @RequestParam String password, HttpServletRequest req){
        return iUserService.logIn(username,password,req.getSession());
    }

    /**
     * 登出
     * @return
     */
    @ApiOperation(value = "登出功能")
    @PostMapping("/out")
    public String logOut(){

        return "success";
    }

    /**
     * 登录成功以后，授权，查看当前用户权限
     */
    @ApiOperation(value = "分级当前用户所有授权")
    @PostMapping("/showPermissionForUserId")
    public List<PermissionVo> showPermissionForUserId(@RequestParam(required = false) HttpServletRequest req,
                                                      @ApiParam(name = "id") @RequestParam(required = false) String id){
        // 有可能会有不存在的情况，session失效情况........
        String uid;
        uid = id;
        if (Objects.isNull(id)){
            HttpSession session = req.getSession();
            uid = session.getAttribute("uid").toString();
        }
        return iPermissionService.queryPermission(uid);
    }



}
